<?php

namespace app\common\service;


class HostLearnData extends Base 

{
    
    /**
     * @name: 列表
     * @Param: tcp=>1,udp=>2
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 16:31:36
     */
    function list ($filterData, $limit)
    {


        $server = db('HostLearnData')
            ->field('id,host_id,protocols,src_ip,dest_ip,src_mac,dest_mac,direction,view,status,have_mac_addr,ctime,dest_port')
            ->where('host_id', $filterData['host_id'])->where('mark', $filterData['mark']);

        isset($filterData['dest_ip']) && !empty($filterData['dest_ip']) ? $server->whereLike('dest_ip', '%' . $filterData['dest_ip'] . '%') : '';
        isset($filterData['src_ip']) && !empty($filterData['src_ip']) ? $server->whereLike('src_ip', '%' . $filterData['src_ip'] . '%') : '';
        isset($filterData['direction']) && is_numeric($filterData['direction']) ? $server->where('direction', $filterData['direction']) : '';
        isset($filterData['dest_port']) && is_numeric($filterData['dest_port']) ? $server->where('dest_port', $filterData['dest_port']) : '';
        isset($filterData['protocols']) && is_numeric($filterData['protocols']) ? $server->where('protocols', $filterData['protocols']) : '';
        isset($filterData['status']) && is_numeric($filterData['status']) ? $server->where('status', $filterData['status']) : '';
        isset($filterData['ctime']) && $filterData['ctime'] > 0 ? $server->where('ctime', '>=', $filterData['ctime'])->where('ctime', '<=', $filterData['ctime'] + 86400) : '';

        $return = $server->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 组列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 15:01:38
     */
    function  groupList ($filterData, $limit)
    {
        $server = db('HostLearnData')->field('host_id,src_ip,dest_ip,mark')->where('host_id', $filterData['host_id']);

        isset($filterData['dest_ip']) && !empty($filterData['dest_ip']) ? $server->whereLike('dest_ip', '%' . $filterData['dest_ip'] . '%') : '';
        isset($filterData['src_ip']) && !empty($filterData['src_ip']) ? $server->whereLike('src_ip', '%' . $filterData['src_ip'] . '%') : '';

        $return = $server->group('mark')->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 统计列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 15:07:09
     */
    function statisticsList ($host_id, $mark, $type = '')
    {

        $server = db('HostLearnData')->whereIn('mark', $mark)->where('host_id', $host_id);

        $type == 'all_view' ? $server->field('mark,SUM(view) as ' . $type) : $server->field('mark,count(*) as ' . $type);
        $type == 'outbount' ? $server->where('direction', 1): '' ;
        $type == 'inbount' ? $server->where('direction', 2): '' ;
        $type == 'have_rule' ? $server->where('status', 1): '' ;

        $data = $server->group('mark')->select();


        $return = array_column($data, $type, 'mark');

        return $return;
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 14:17:27
     */
    function delete ($host_id, $learn_id)
    {
        db('HostLearnData')->where('host_id', $host_id)->whereIn('id', $learn_id)->delete();
    }

    /**
     * @name: 创建规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 16:16:43
     */
    function createRule ($host_id, $learn_id)
    {
        db('HostLearnData')->where('host_id', $host_id)->whereIn('id', $learn_id)->update(['status' => 1]);
        $config = db('HostLearnConfig')->where('host_id', $host_id)->find();
        $HostRule = new HostRule();
        $HostRule->toDo('insertFromLearnData',[$host_id, $learn_id, $config['allow'], $config['have_mac_addr']]);
    }

    /**
     * @name: 通过 学习id 查询
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-16 18:14:56
     */
    function selectByLearnId ($host_id, $learn_id)
    {
        $learnData = db('HostLearnData')->alias('hld')
            ->join('HostLearnConfig hlf','hld.host_id = hlf.host_id')
            ->where('hld.host_id', $host_id)->whereIn('hld.id', $learn_id)
            ->field('hld.*, hlf.allow, hlf.have_mac_addr')
            ->select();

        if(empty($learnData))
        {
            $this->setError($this->CODE_FAIL, '不存在的学习数据');
            return [];
        }
        
        return $learnData;
    }

    /**
     * @name: 发送到客户端格式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-16 18:19:21
     */
    function clientRule ($rule)
    {
        $return = [
            'alias' => '' ,
            'direction' => $rule['direction'],
            'protocols' => $rule['protocols'],
            'process' => $rule['allow'],
            'dest_port' => $rule['dest_port'],
            'src_port' => 'any',
            'src_ip' => $rule['src_ip'],
            'dest_ip' => $rule['dest_ip'],
            'mac_addr' => $rule['direction'] == 1 ? $rule['dest_mac'] : $rule['src_mac'],
            'have_mac_addr' => $rule['have_mac_addr'],
        ];

        return $return;
    }

    /**
     * @name: 规则删除后 返回状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-30 16:53:59
     */
    function resetStatus ($host_id, $learn_id)
    {
        if(empty($learn_id))
        {
            return false;
        }
        
        db('HostLearnData')->where('host_id', $host_id)->whereIn('id', $learn_id)->update(['status' => 0]);
    }
}